//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package io.paperdb;

import com.esotericsoftware.kryo.Serializer;
import ohos.app.Context;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public class Paper {
    static final String TAG = "paperdb";
    static final String DEFAULT_DB_NAME = "io.paperdb";
    private static Context mContext;
    private static final ConcurrentHashMap<String, Book> mBookMap = new ConcurrentHashMap();
    private static final HashMap<Class, Serializer> mCustomSerializers = new HashMap();

    public Paper() {
    }

    public static void init(Context context) {
        mContext = context.getApplicationContext();
    }

    public static Book book(String name) {
        if (name.equals("io.paperdb")) {
            throw new PaperDbException("io.paperdb name is reserved for default library name");
        } else {
            return getBook(name);
        }
    }

    public static Book book() {
        return getBook("io.paperdb");
    }

    private static Book getBook(String name) {
        if (mContext == null) {
            throw new PaperDbException("Paper.init is not called");
        } else {
            synchronized(mBookMap) {
                Book book = (Book)mBookMap.get(name);
                if (book == null) {
                    book = new Book(mContext, name, mCustomSerializers);
                    mBookMap.put(name, book);
                }

                return book;
            }
        }
    }

    /** @deprecated */
    public static <T> Book put(String key, T value) {
        return book().write(key, value);
    }

    /** @deprecated */
    public static <T> T get(String key) {
        return book().read(key);
    }

    /** @deprecated */
    public static <T> T get(String key, T defaultValue) {
        return book().read(key, defaultValue);
    }

    /** @deprecated */
    public static boolean exist(String key) {
        return book().exist(key);
    }

    /** @deprecated */
    public static void delete(String key) {
        book().delete(key);
    }

    /** @deprecated */
    public static void clear(Context context) {
        init(context);
        book().destroy();
    }

    public static void setLogLevel(int level) {
        Iterator var1 = mBookMap.entrySet().iterator();

        while(var1.hasNext()) {
            Entry<String, Book> entry = (Entry)var1.next();
            ((Book)entry.getValue()).setLogLevel(level);
        }

    }

    public static <T> void addSerializer(Class<T> clazz, Serializer<T> serializer) {
        if (!mCustomSerializers.containsKey(clazz)) {
            mCustomSerializers.put(clazz, serializer);
        }

    }
}
